package org.pms.demo.webservice.service.impl;

import org.apache.commons.collections.map.HashedMap;
import org.pms.demo.webservice.dao.pms.ResourceMapper;
import org.pms.demo.webservice.domain.Resource;
import org.pms.demo.webservice.service.ResourceVersionService;
import org.pms.demo.webservice.utils.ProjectUtils;
import org.pms.demo.webservice.utils.StringUtils;
import org.pms.demo.webservice.vo.App;
import org.pms.demo.webservice.vo.ResourceInfo;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by xusaike on 17/6/13.
 */
@Service
public class ResourceVersionServiceImpl implements ResourceVersionService {
    @javax.annotation.Resource
    private ResourceMapper resourceMapper;
    @Override
    public String getResoureListByUser() {
       /* Subject currentUser = SecurityUtils.getSubject();//获取当前用户
        String username = (String)currentUser.getPrincipal();*/
        int app_id = 1;
        List<ResourceInfo> list = new ArrayList<>();
        List<Resource> resources = resourceMapper.getResourceTreeByAppid(app_id);
        if(resources.size()>0){
            Resource resource = resources.get(0);
            ResourceInfo resourceInfo = new ResourceInfo();

            resourceInfo.setName(resource.getName());
            resourceInfo.setRoute(resource.getRoute());
            resourceInfo.setType(resource.getResource_type());

            getResourceTree(resources,resourceInfo,resources.get(0),1);
            list.add(resourceInfo);

        }
        return ProjectUtils.getContentMsg(list);
    }
    @Override
    public String getAllResoureList() {
        List<Resource> resources = resourceMapper.getAllResource();


        Map<String,Object> map = new HashMap();



        Map<String,Object> resource_map = new HashMap();
        List<App> app_list = new ArrayList<>();
        List<Object> list = new ArrayList();
        int temp_app=0;
        int rownumber=1;
        for(int i=0;i<resources.size();i++){
            Resource resource = resources.get(i);
            //ResourceInfo resourceInfo = new ResourceInfo();
            Map<String ,Object> resourceInfo = new HashedMap();


            resourceInfo.put("name",resource.getName());
            resourceInfo.put("id",resource.getId()+"");
            resourceInfo.put("parent_ids",resource.getParent_ids());
            resourceInfo.put("node_type",resource.getResource_type());
            resourceInfo.put("parent_id",resource.getParent_id()+"");
            resourceInfo.put("pName",resource.getpName());
            resourceInfo.put("permission",resource.getPermission());
            resourceInfo.put("route",resource.getRoute());
            resourceInfo.put("url",resource.getUrl());
            resourceInfo.put("available",resource.getAvailable());


            if(resource.getParent_id()!=0L){
                resourceInfo.put("_parentId",resource.getParent_id());
                //resourceInfo.put("state","closed");
            }else{

            }
            //节点开始
            if(temp_app!=resource.getApp_id()){
                rownumber=1;
                App app = new App();
                app.setApp_id(resource.getApp_id()+"");
                app.setApp_name(resource.getApp_name());
                app_list.add(app);
                if(temp_app!=0){
                    resource_map.put(temp_app+"",list);
                    list = new ArrayList<>();
                }
                temp_app=resource.getApp_id();

            }
            list.add(resourceInfo);


        }
        if(temp_app!=0){
            resource_map.put(temp_app+"",list);
        }

        map.put("app_list",app_list);
        map.put("list",resource_map);
        return ProjectUtils.getContentMsg(map);
    }

    @Override
    public String addResource(int pid, String name, String permission, String node_type, String url, String route) {
        if(pid==0){
            return ProjectUtils.getErrorMsg("参数错误：父节点");
        }
        if(StringUtils.isNullOrEmpty(name)){
            return ProjectUtils.getErrorMsg("参数错误：新建节点不能为空");
        }
        if(StringUtils.isNullOrEmpty(permission)){
            return ProjectUtils.getErrorMsg("参数错误：权限不能为空");
        }
        if(StringUtils.isNullOrEmpty(node_type)){
            return ProjectUtils.getErrorMsg("参数错误：节点类型不能为空");
        }else if(!("menu".equals(node_type)||"button".equals(node_type))){
            return ProjectUtils.getErrorMsg("参数错误：节点类型有误");
        }
        Resource pResource = resourceMapper.getResourceById(pid);
        if(pResource==null){
            return ProjectUtils.getErrorMsg("参数错误：父节点不存在");
        }
        Resource resourceInfo = new Resource();
        resourceInfo.setName(name);
        resourceInfo.setParent_ids(pResource.getParent_ids()+pResource.getId()+"/");
        resourceInfo.setResource_type(node_type);
        resourceInfo.setParent_id(pResource.getId());
        resourceInfo.setPermission(permission);
        resourceInfo.setRoute(route);
        resourceInfo.setUrl(url);
        resourceInfo.setAvailable(1);
        resourceInfo.setApp_id(pResource.getApp_id());
        int ret = resourceMapper.addResource(resourceInfo);
        if(ret==1){
            Map<String,Object> tokenMap = new HashedMap();
            Map<String,Object> content = new HashedMap();
            content.put("id",resourceInfo.getId());
            tokenMap.put("object",content);
            return ProjectUtils.getContentMsg("添加成功",tokenMap);
        }
        return ProjectUtils.getErrorMsg("添加失败");
    }

    @Override
    public String updateResource(int id, String name, String permission, String node_type, String url, String route,int available,int type) {
        if(id==0){
            return ProjectUtils.getErrorMsg("参数错误：节点不存在");
        }
        Resource resourceInfo = resourceMapper.getResourceById(id);
        if(resourceInfo==null){
            return ProjectUtils.getErrorMsg("参数错误：节点不存在");
        }
        if(type==2){//启用/禁用节点
            if(available==1||available==2){
                resourceInfo.setAvailable(available);
            }
        }else{
            if(StringUtils.isNullOrEmpty(name)){
                return ProjectUtils.getErrorMsg("参数错误：节点名不能为空");
            }
            if(StringUtils.isNullOrEmpty(permission)){
                return ProjectUtils.getErrorMsg("参数错误：权限不能为空");
            }
            if(StringUtils.isNullOrEmpty(node_type)){
                return ProjectUtils.getErrorMsg("参数错误：节点类型不能为空");
            }else if(!("menu".equals(node_type)||"button".equals(node_type))){
                return ProjectUtils.getErrorMsg("参数错误：节点类型有误");
            }
            resourceInfo.setName(name);
            resourceInfo.setResource_type(node_type);
            resourceInfo.setPermission(permission);
            resourceInfo.setRoute(route);
            resourceInfo.setUrl(url);
        }
        int ret = resourceMapper.updateResource(resourceInfo);
        if(ret==1){
            Map<String,Object> tokenMap = new HashedMap();
            Map<String,Object> content = new HashedMap();
            content.put("id",resourceInfo.getId());
            tokenMap.put("object",content);
            return ProjectUtils.getContentMsg("修改成功",tokenMap);
        }
        return ProjectUtils.getErrorMsg("修改失败");
    }

    @Override
    public String deleteResource(int id) {
        List<Resource> list = resourceMapper.getResourceByPId(id);
        if(list==null||list.size()==0){
            int ret = resourceMapper.deleteResource(id);
            if(ret==1){
                return ProjectUtils.getContentMsg("删除成功");
            }
        }else{
            return ProjectUtils.getErrorMsg("该节点拥有子节点，无法删除");
        }

        return ProjectUtils.getErrorMsg("删除失败");

    }

    private void getResourceTree(List<Resource> resources,ResourceInfo pre_resourceInfo ,Resource parent,int i){

        for(;i<resources.size();i++ ) {
            Resource resource = resources.get(i);
            ResourceInfo resourceInfo = new ResourceInfo();
            resourceInfo.setName(resource.getName());
            resourceInfo.setRoute(resource.getRoute());
            resourceInfo.setType(resource.getResource_type());
           if (parent.getId().equals(resource.getParent_id())) {
               pre_resourceInfo.getList().add(resourceInfo);
            } else {
              // getResourceTree(resources,resourceInfo ,resource,i);
            }


        }

    }

}
